package com.cykj.faceledtv.ui.splash;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cykj.citypicker.CityPicker;
import com.cykj.citypicker.adapter.OnPickListener;
import com.cykj.citypicker.model.City;
import com.cykj.faceledtv.MyApp;
import com.cykj.faceledtv.R;
import com.cykj.faceledtv.common.Constants;
import com.cykj.faceledtv.ui.base.BaseActivity;
import com.cykj.faceledtv.ui.main.MainActivity;
import com.cykj.faceledtv.widget.FocusLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jzh
 * @date 2019/05/09 08:54
 */
public class SplashActivity extends BaseActivity {
    @BindView(R.id.ac_splash_address)
    TextView address;

    private String city;
    private boolean isShowDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_splash;
    }

    @Override
    protected void initWidget() {
        setUnBinder(ButterKnife.bind(this));
        city = MyApp.getInstance().mDataManager.getAddress();
        address.setText(city);
        FocusLayout mFocusLayout = new FocusLayout(this);
        //获取根元素
        View mContainerView = this.getWindow().getDecorView();//.findViewById(android.R.id.content);
        //得到整个view树的viewTreeObserver
        ViewTreeObserver viewTreeObserver = mContainerView.getViewTreeObserver();
        //给观察者设置焦点变化监听
        viewTreeObserver.addOnGlobalFocusChangeListener(mFocusLayout);
        addContentView(mFocusLayout,
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));//添加焦点层
    }


    @OnClick({R.id.ac_splash_setting, R.id.ac_splash_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_splash_setting:
                doCityPicker();
                break;
            case R.id.ac_splash_next:
                if (!hasAllPermissions(Constants.NEED_PERMISSIONS)) {
                    if (isShowDialog) {
                        checkPermissions(Constants.NEED_PERMISSIONS);
                    } else {
                        new AlertDialog.Builder(this).setTitle(getString(R.string.tips))
                                .setMessage("应用将要获取基本权限，" +
                                        "我们保证以下权限为刚需权限，请放心授予")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        checkPermissions(Constants.NEED_PERMISSIONS);
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                checkPermissions(Constants.NEED_PERMISSIONS);
                            }
                        }).setCancelable(false).create().show();
                        isShowDialog = true;
                    }
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString(Constants.PARAMS, city);
                goActivity(MainActivity.class, bundle);
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 城市选择
     */
    private void doCityPicker() {
        CityPicker.from(this) //activity或者fragment
                .enableAnimation(true)    //启用动画效果，默认无
                .setHotCities(null)    //指定热门城市
                .setOnPickListener(new OnPickListener() {
                    @Override
                    public void onPick(int position, City data) {
                        city = data.getName();
                        address.setText(data.getName());
                        MyApp.getInstance().mDataManager.setAddress(city);
                    }

                    @Override
                    public void onCancel() {

                    }

                })
                .show();
    }

}
